/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.veditor.view;

import java.awt.Component;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.text.AttributeSet;
import javax.swing.text.Element;
import javax.swing.text.html.HTML;
import javax.swing.tree.DefaultMutableTreeNode;
import offset.nodes.client.editor.model.StructureTree;
import offset.nodes.client.editor.view.HeadingRenderer;
import offset.nodes.client.editor.view.StructureRenderer;
import offset.nodes.client.veditor.model.DataModel;

public class VirtualStructureRenderer extends StructureRenderer {

    static ImageIcon xmlIcon = new ImageIcon(HeadingRenderer.class.getResource("/offset/nodes/client/editor/view/resources/sc_xml.png"));
    static ImageIcon xslIcon = new ImageIcon(HeadingRenderer.class.getResource("/offset/nodes/client/editor/view/resources/sc_xsl.png"));
    static ImageIcon htmlXmlIcon = new ImageIcon(HeadingRenderer.class.getResource("/offset/nodes/client/editor/view/resources/sc_html_xml.png"));
    public static final String separator = "/";

    public VirtualStructureRenderer(DataModel model) {
        super(model);
    }

    protected String getXMLName(String xml) {
        if (xml.indexOf(separator) < 0)
            return xml;
        return xml.substring(xml.lastIndexOf(separator) + 1);
    }

    protected String getTemplateName(String xsl) {
        if (xsl.indexOf(File.separator) < 0)
            return xsl;
        return xsl.substring(xsl.lastIndexOf(File.separator) + 1);
    }

    public void setXSL(Element element, String xmlPath, String xslPath) {
        StringBuffer buf = new StringBuffer();

        buf.append("<html>" + element.getName());
        if (xmlPath != null)
            buf.append(" <font color=gray>" + getXMLName(xmlPath) + "</font>");
        buf.append(" <font color=blue>" + getTemplateName(xslPath) + "</font>");

        setText(buf.toString());
        setToolTipText(xmlPath);
        setIcon(xslIcon);
    }

    public void setXML(Element element, String xmlPath, boolean editable, String action) {
        StringBuilder buf = new StringBuilder();
        buf.append("<html>" + element.getName() + " <font color=gray>");
        if (!editable)
            buf.append("<i>");
        buf.append(getXMLName(xmlPath));
        if (!editable)
            buf.append("</i>");
        buf.append("</font>");
        if (action != null)
            buf.append(" <font color=green>" + action + "</font>");
        buf.append("</html>");
        setText(buf.toString());
        setToolTipText(xmlPath);
        setIcon(htmlXmlIcon);
    }
    
    public void setAction(Element element, String action) {
        StringBuilder buf = new StringBuilder();
        buf.append("<html>" + element.getName());
        if (action != null)
            buf.append(" <font color=green>" + action + "</font>");
        buf.append("</html>");
        setText(buf.toString());
        setToolTipText(action);
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean sel,
            boolean expanded,
            boolean leaf, int row,
            boolean hasFocus) {
        Component ret = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
        if (!(treeNode.getUserObject() instanceof Element))
            return ret;
        Element element = (Element) treeNode.getUserObject();
        if (getModel().isArtificialWithInlineElement(element))
            element = getModel().getHtmlElement(element).getElement(0);

        if (element.getName().equals(HTML.Tag.CONTENT.toString())) {
            setIcon(contentIcon);

            AttributeSet dfn = (AttributeSet) element.getAttributes().getAttribute(DataModel.RUN_TAG);
            if (dfn != null) {
                String xml = (String) dfn.getAttribute(DataModel.ATTRIBUTE_NAME_DATA);
                String action =  (String) dfn.getAttribute(DataModel.ATTRIBUTE_NAME_ACTION);
                if (xml != null)
                    setXML(element, xml, true, action);
                else
                    setText("");
            } else
                setText("");
        } else if (element.getAttributes().isDefined(DataModel.ATTRIBUTE_NAME_TEMPLATE)) {
            String xml = (String) element.getAttributes().getAttribute(DataModel.ATTRIBUTE_NAME_DATA);
            String xsl = (String) element.getAttributes().getAttribute(DataModel.ATTRIBUTE_NAME_TEMPLATE);
            setXSL(element, xml, xsl);
        } else if (element.getAttributes().isDefined(DataModel.ATTRIBUTE_NAME_DATA)) {
            String xml = (String) element.getAttributes().getAttribute(DataModel.ATTRIBUTE_NAME_DATA);
            boolean editable = true;
            if (element.getAttributes().isDefined(DataModel.ATTRIBUTE_NAME_EDIT))
                if (element.getAttributes().getAttribute(DataModel.ATTRIBUTE_NAME_EDIT).equals(DataModel.ATTRIBUTE_VALUE_FALSE))
                    editable = false;
            String action = (String) element.getAttributes().getAttribute(DataModel.ATTRIBUTE_NAME_ACTION);
            setXML(element, xml, editable, action);
        } else if (element instanceof StructureTree.XMLElement) {
            setText("<html><font color=gray>" + ((StructureTree.XMLElement) element).getName() + "</font></html>");
            setIcon(xmlIcon);
        } else {
            if (element.getAttributes().isDefined(DataModel.ATTRIBUTE_NAME_ACTION)) {
                String action = (String) element.getAttributes().getAttribute(DataModel.ATTRIBUTE_NAME_ACTION);
                setAction(element, action);
            }
            else
                setText(element.getName());
            setIcon(elementIcon);
        }

        return ret;
    }
}
